package com.woniu.cloud.storeassembly.apiimpl;

import com.woniu.cloud.api.StorePurchaseCarApi;
import com.woniu.cloud.common.dto.Result;
import com.woniu.cloud.entity.TStorePurchaseCar;
import com.woniu.cloud.form.InsertTStorePurchaseCar;
import com.woniu.cloud.form.UpdateStorePurchaseCar;
import com.woniu.cloud.storeassembly.service.ITStorePurchaseCarService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 平台对店铺商品表 前端控制器
 * </p>
 *
 * @author hyy
 * @since 2021-09-22
 */
@RestController
@Slf4j
public class StorePurchaseCarApiImpl implements StorePurchaseCarApi {


    @Resource
    private ITStorePurchaseCarService storePurchaseCarService;


    @Override
    @GetMapping("getStorePurchaseCarByStoreId")
    @GlobalTransactional
    public Result getStorePurchaseCarByStoreId(@RequestParam("storeId") Integer storeId) {
        log.info("通过店铺Id查找->storeId:{}",storeId);
        if (null==storeId) {
            return Result.fail("参数异常",null);
        }
        try {
            log.info("开始查询->storeId:{}",storeId);
            return storePurchaseCarService.getStorePurchaseCarByStoreId(storeId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("服务异常",null);
        }
    }

    @Override
    @PostMapping("insertTStorePurchaseCar")
    @GlobalTransactional
    public Result insertTStorePurchaseCar(@RequestBody InsertTStorePurchaseCar insertTStorePurchaseCar) {
        log.info("添加购物车->insertTStorePurchaseCar:{}",insertTStorePurchaseCar);
        if (null==insertTStorePurchaseCar) {
            return Result.fail("参数异常",null);
        }
        try {
            TStorePurchaseCar storePurchaseCar = new TStorePurchaseCar();
            BeanUtils.copyProperties(insertTStorePurchaseCar,storePurchaseCar);
            log.info("开始添加->store:{}",storePurchaseCar);
            return storePurchaseCarService.insertTStorePurchaseCar(storePurchaseCar);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("服务异常",null);
        }
    }

    @Override
    @PutMapping("updateStorePurchaseCar")
    @GlobalTransactional
    public Result updateStorePurchaseCar(@RequestBody UpdateStorePurchaseCar updateStorePurchaseCar) {
        log.info("修改购物车信息->updateStorePurchaseCar{}",updateStorePurchaseCar);
        if (null==updateStorePurchaseCar) {
            return Result.fail("参数异常",null);
        }
        try {
            TStorePurchaseCar storePurchaseCar = new TStorePurchaseCar();
            BeanUtils.copyProperties(updateStorePurchaseCar,storePurchaseCar);
            log.info("开始修改->storePurchaseCar:{}",storePurchaseCar);
            return storePurchaseCarService.updateStorePurchaseCar(storePurchaseCar);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("服务异常",null);
        }
    }

    @Override
    @DeleteMapping("deleteStorePurchaseCar")
    @GlobalTransactional
    public Result deleteStorePurchaseCar(@RequestParam("storePurchaseCarId") Integer storePurchaseCarId) {
        log.info("根据id删除购物车->storePurchaseCarId:{}",storePurchaseCarId);
        if (null==storePurchaseCarId) {
            return Result.fail("参数异常",null);
        }
        try {
            log.info("开始删除->goodId:{}",storePurchaseCarId);
            return storePurchaseCarService.deleteStorePurchaseCar(storePurchaseCarId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("服务异常",false);
        }
    }
}
